package com.sptpc.qimoxiangmu.controller;

import com.sptpc.qimoxiangmu.controller.request.OrderRequest;
import com.sptpc.qimoxiangmu.service.OrderService;
import com.sptpc.qimoxiangmu.util.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;

    //显示的页面，成功
//    @GetMapping("/list.html")
//    public ModelAndView listView(){
//        ModelAndView mv =new ModelAndView("order/list");
//        List<Order> orderList=orderService.findAll();
//        mv.addObject("orderList",orderList);
//        return mv;
//    }
//    //名字查询order对象
//    @GetMapping("/list_view_name")
//    public ModelAndView listView(String customer){
//        ModelAndView mv =new ModelAndView("order/list");
//        List<OrderDetailVo> orderList= orderService.findOrderBycustomer(customer);
//        mv.addObject("orderList",orderList);
//        return mv;
//    }


//    //编辑后提交保存，成功
//    @GetMapping("/update_view")
//    public  String updateView(@RequestParam(name = "id")Long id,@RequestParam(name = "newName")String name){
//        categoryService.update(id,name);
//        return "redirect:list_view";
//    }
    //成功，插入
    @PostMapping(value = "/save",consumes = MediaType.APPLICATION_JSON_VALUE)
    public ServerResponse save(@RequestBody OrderRequest orderRequest){//因为是json所以要加@RequestBody
        return  orderService.save(orderRequest);
    }
    //查询所有order的内容，成功
    @GetMapping("/")
    public  ServerResponse listByCustomer(){
        return ServerResponse.createBySuccess(orderService.findAllOrder());
    }
    //通过customer对order表查询，成功
    @GetMapping(value = "/",params = "customer")
    public ServerResponse list(@RequestParam String customer){
        return ServerResponse.createBySuccess(orderService.findOrderBycustomer(customer));
    }  //通过drinkname对表进行查询，成功
    @GetMapping(value = "/",params = "modelname")
    public  ServerResponse listByDrinkName(@RequestParam String modelname){
        return ServerResponse.createBySuccess(orderService.finOrderByModelname(modelname));
    }

    //通过drink和category名字查询，成功
    @GetMapping(value = "/",params = {"type","modelName"})
    public  ServerResponse listfindOrderAllByCustomerAndDrinkName(String type, String modelName){
        return ServerResponse.createBySuccess(orderService.findAllByCustomerAndDrinkName(type,modelName));
    }
    //时间段查询，成功
    @GetMapping(value = "/",params = {"startTime","endTime"})
    public  ServerResponse listByCreateTime(@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime startTime,
                                            @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime){
        return ServerResponse.createBySuccess(orderService.findAllByCreateTimeBetween(startTime,endTime));
    }

}





